//
//  ShopEditBottomView.m
//  JewelryStore
//
//  Created by apple on 2017/1/9.
//  Copyright © 2017年 com. All rights reserved.
//

#import "ShopEditBottomView.h"
#import "UIButton+RoundedRect.h"

IB_DESIGNABLE

@interface ShopEditBottomView ()

@property (nonatomic, strong) UIButton *allSelectedBtn;

@end

@implementation ShopEditBottomView

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    
    if (self) {
        [self configure];
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self configure];
        
    }
    return self;

}


- (void)configure
{
    CGFloat edges = SCREEN_WIDTH/15;
    CGFloat totalWidth = (SCREEN_WIDTH - 80 - 4*edges);
    CGFloat width = totalWidth/3;
    CGFloat height = width/2;

    CGRect backFrame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    UIImageView *backView = [[UIImageView alloc] initWithFrame:backFrame];
    backView.image = KImageNamed(@"bottomBV");
    backView.userInteractionEnabled = YES;
    [self addSubview:backView];
    
    //勾选
    UIButton *selectAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectAllBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    selectAllBtn.userInteractionEnabled = YES;
    selectAllBtn.titleLabel.font = Font(16);
    [selectAllBtn setImage:KImageNamed(@"m_noselect") forState:UIControlStateNormal];
    [selectAllBtn setTitle:@"  全选" forState:UIControlStateNormal];
    [selectAllBtn addTarget:self action:@selector(selectAllClick:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:selectAllBtn];
    [selectAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.top.equalTo(@0);
        make.width.equalTo(@80);
        make.height.equalTo(@(SCREEN_WIDTH/5.5));
    }];
    
    //分享
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    shareBtn.titleLabel.font = Font(16);
    [shareBtn addTarget:self action:@selector(shareClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:shareBtn];
    [shareBtn setupRoundeRectWithWidth:width];
    [shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(selectAllBtn);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
        make.left.equalTo(selectAllBtn.mas_right).offset(edges);
    }];
    
    //收藏
    UIButton *favoriteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [favoriteBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [favoriteBtn setTitle:@"收藏" forState:UIControlStateNormal];
    favoriteBtn.titleLabel.font = Font(16);
    [favoriteBtn addTarget:self action:@selector(favoriteClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:favoriteBtn];
    [favoriteBtn setupRoundeRectWithWidth:width];
    [favoriteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(selectAllBtn);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
        make.left.equalTo(shareBtn.mas_right).offset(edges);
    }];

    //删除
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    deleteBtn.titleLabel.font = Font(16);
    [deleteBtn addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:deleteBtn];
    [deleteBtn setupRoundeRectWithWidth:width];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(selectAllBtn);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
        make.left.equalTo(favoriteBtn.mas_right).offset(edges);
    }];

    __block UIButton *btn = selectAllBtn;
    
    self.updateBtn = ^(BOOL isAllSelected)
    {
        if (isAllSelected)
        {
            [btn setImage:KImageNamed(@"s_select") forState:UIControlStateNormal];
        }else
        {
            [btn setImage:KImageNamed(@"m_noselect") forState:UIControlStateNormal];
        }
    };
}

#pragma maek - prvite method

- (void)selectAllClick:(UIButton *)button
{
    [self operateMethod:SHOPEDITTYPE_SELECTED];
}

- (void)shareClick
{
    [self operateMethod:SHOPEDITTYPE_SHARE];
}

- (void)favoriteClick
{
    [self operateMethod:SHOPEDITTYPE_FOVARITY];
}

- (void)deleteBtnClick
{
    [self operateMethod:SHOPEDITTYPE_DELETE];
}

- (void)operateMethod:(SHOPEDITTYPE)type
{
    if (self.shopEditDelegate && [self.shopEditDelegate respondsToSelector:@selector(operateMethodWithType:)]) {
        [self.shopEditDelegate operateMethodWithType:type];
    }
}
@end
